package com.qmyx.supply.admin.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
@Configuration
public class RabbitMqConfiguration {

    @Bean(name="guchuangConnectionFactory")
    @Primary
    public ConnectionFactory guchuangConnectionFactory(
            @Value("${spring.rabbitmq.guchuang.host}") String host,
            @Value("${spring.rabbitmq.guchuang.username}") String username,
            @Value("${spring.rabbitmq.guchuang.password}") String password,
            @Value("${spring.rabbitmq.guchuang.virtual-host}") String virtualHost){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(5672);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);

        return connectionFactory;
    }

//    @Bean(name="jumaiduoConnectionFactory")
//    public ConnectionFactory jumaiduoConnectionFactory(
//            @Value("${spring.rabbitmq.jumaiduo.host}") String host,
//            @Value("${spring.rabbitmq.jumaiduo.username}") String username,
//            @Value("${spring.rabbitmq.jumaiduo.password}") String password,
//            @Value("${spring.rabbitmq.jumaiduo.virtual-host}") String virtualHost){
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setHost(host);
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setVirtualHost(virtualHost);
//        return connectionFactory;
//    }
//
//    @Bean(name="yiquanConnectionFactory")
//    public ConnectionFactory yiquanConnectionFactory(
//            @Value("${spring.rabbitmq.yiquan.host}") String host,
//            @Value("${spring.rabbitmq.yiquan.username}") String username,
//            @Value("${spring.rabbitmq.yiquan.password}") String password,
//            @Value("${spring.rabbitmq.yiquan.virtual-host}") String virtualHost){
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setHost(host);
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setVirtualHost(virtualHost);
//        return connectionFactory;
//    }
//
//    @Bean(name="zxlpConnectionFactory")
//    public ConnectionFactory zxlpConnectionFactory(
//            @Value("${spring.rabbitmq.zxlp.host}") String host,
//            @Value("${spring.rabbitmq.zxlp.username}") String username,
//            @Value("${spring.rabbitmq.zxlp.password}") String password,
//            @Value("${spring.rabbitmq.zxlp.virtual-host}") String virtualHost){
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setHost(host);
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setVirtualHost(virtualHost);
//        return connectionFactory;
//    }
//
//    @Bean(name="ygyxConnectionFactory")
//    public ConnectionFactory ygyxConnectionFactory(
//            @Value("${spring.rabbitmq.ygyx.host}") String host,
//            @Value("${spring.rabbitmq.ygyx.username}") String username,
//            @Value("${spring.rabbitmq.ygyx.password}") String password,
//            @Value("${spring.rabbitmq.ygyx.virtual-host}") String virtualHost){
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setHost(host);
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setVirtualHost(virtualHost);
//        return connectionFactory;
//    }
//
//    @Bean(name="yxhdConnectionFactory")
//    public ConnectionFactory yxhdConnectionFactory(
//            @Value("${spring.rabbitmq.yxhd.host}") String host,
//            @Value("${spring.rabbitmq.yxhd.username}") String username,
//            @Value("${spring.rabbitmq.yxhd.password}") String password,
//            @Value("${spring.rabbitmq.yxhd.virtual-host}") String virtualHost){
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setHost(host);
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setVirtualHost(virtualHost);
//        return connectionFactory;
//    }
//
//    @Bean(name="jckjConnectionFactory")
//    public ConnectionFactory jckjConnectionFactory(
//            @Value("${spring.rabbitmq.jckj.host}") String host,
//            @Value("${spring.rabbitmq.jckj.username}") String username,
//            @Value("${spring.rabbitmq.jckj.password}") String password,
//            @Value("${spring.rabbitmq.jckj.virtual-host}") String virtualHost){
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setHost(host);
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setVirtualHost(virtualHost);
//        return connectionFactory;
//    }


    @Bean(name="guchuangRabbitTemplate")
    @Primary
    public RabbitTemplate firstRabbitTemplate(@Qualifier("guchuangConnectionFactory") ConnectionFactory connectionFactory){
        RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
//        firstRabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //使用外部事物
        //ydtRabbitTemplate.setChannelTransacted(true);
        return firstRabbitTemplate;
    }

//    @Bean(name="jumaiduoRabbitTemplate")
////    @Primary
//    public RabbitTemplate secondRabbitTemplate(@Qualifier("jumaiduoConnectionFactory") ConnectionFactory connectionFactory){
//        RabbitTemplate secondRabbitTemplate = new RabbitTemplate(connectionFactory);
//        //使用外部事物
//        //lpzRabbitTemplate.setChannelTransacted(true);
////        secondRabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return secondRabbitTemplate;
//    }
//
//    @Bean(name="yiquanRabbitTemplate")
//    public RabbitTemplate threeRabbitTemplate(@Qualifier("yiquanConnectionFactory") ConnectionFactory connectionFactory){
//        RabbitTemplate threeRabbitTemplate = new RabbitTemplate(connectionFactory);
//        //使用外部事物
//        //lpzRabbitTemplate.setChannelTransacted(true);
////        threeRabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return threeRabbitTemplate;
//    }
//
//
//    @Bean(name="zxlpRabbitTemplate")
//    public RabbitTemplate fourRabbitTemplate(@Qualifier("zxlpConnectionFactory") ConnectionFactory connectionFactory){
//        RabbitTemplate fourRabbitTemplate = new RabbitTemplate(connectionFactory);
//        //使用外部事物
//        //lpzRabbitTemplate.setChannelTransacted(true);
////        threeRabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return fourRabbitTemplate;
//    }
//
//    @Bean(name="ygyxRabbitTemplate")
//    public RabbitTemplate fiveRabbitTemplate(@Qualifier("ygyxConnectionFactory") ConnectionFactory connectionFactory){
//        RabbitTemplate fiveRabbitTemplate = new RabbitTemplate(connectionFactory);
//        //使用外部事物
//        //lpzRabbitTemplate.setChannelTransacted(true);
////        threeRabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return fiveRabbitTemplate;
//    }
//
//    @Bean(name="yxhdRabbitTemplate")
//    public RabbitTemplate sixRabbitTemplate(@Qualifier("yxhdConnectionFactory") ConnectionFactory connectionFactory){
//        RabbitTemplate sixRabbitTemplate = new RabbitTemplate(connectionFactory);
//        //使用外部事物
//        //lpzRabbitTemplate.setChannelTransacted(true);
////        threeRabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return sixRabbitTemplate;
//    }
//
//    @Bean(name="jckjRabbitTemplate")
//    public RabbitTemplate sevenRabbitTemplate(@Qualifier("jckjConnectionFactory") ConnectionFactory connectionFactory){
//        RabbitTemplate sevenRabbitTemplate = new RabbitTemplate(connectionFactory);
//        //使用外部事物
//        //lpzRabbitTemplate.setChannelTransacted(true);
////        threeRabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return sevenRabbitTemplate;
//    }

    @Bean(name="guchuangRabbitListenerContainer")
    public SimpleRabbitListenerContainerFactory guchuangRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("guchuangConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }


//    @Bean(name="jumaiduoRabbitListenerContainer")
//    public SimpleRabbitListenerContainerFactory jumaiduoRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
//            @Qualifier("jumaiduoConnectionFactory") ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
//
//    @Bean(name="yiquanRabbitListenerContainer")
//    public SimpleRabbitListenerContainerFactory yiquanRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
//                                                            @Qualifier("yiquanConnectionFactory") ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
//
//    @Bean(name="zxlpRabbitListenerContainer")
//    public SimpleRabbitListenerContainerFactory zxlpRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
//                                                                              @Qualifier("zxlpConnectionFactory") ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
//
//    @Bean(name="ygyxRabbitListenerContainer")
//    public SimpleRabbitListenerContainerFactory ygyxRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
//                                                                            @Qualifier("ygyxConnectionFactory") ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
//
//    @Bean(name="yxhdRabbitListenerContainer")
//    public SimpleRabbitListenerContainerFactory yxhdRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
//                                                                            @Qualifier("yxhdConnectionFactory") ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
//
//    @Bean(name="jckjRabbitListenerContainer")
//    public SimpleRabbitListenerContainerFactory jckjRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
//                                                                            @Qualifier("jckjConnectionFactory") ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
}
